package cn.kgc.author.controller;

import cn.kgc.author.service.AuthorService;
import cn.kgc.common.entity.Author;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by teacher ZHANG on 2023/2/2
 */
@RestController
public class AuthorController {
    @Autowired
    private AuthorService authorService;

    @GetMapping("/authors/{authorIds}")
    public List<Author> findByIds(@PathVariable("authorIds") String authorIds) {
        String[] array = authorIds.split(",");

        List<Integer> idList = new ArrayList<>();
        for (int i = 0; i < array.length; i++) {
            idList.add(Integer.parseInt(array[i]));
        }

        return authorService.listByIds(idList);
    }

    @GetMapping("/authors")
    public Author findAuthor(String username, String phone) {
        if (username != null) {
            return authorService.findByUsername(username);
        }
        else {
            return authorService.findByPhone(phone);
        }
    }
}
